Security News
Bun 1.2 Released with 90% Node.js Compatibility and Built-in S3 Object Support
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.
resolve-from
Advanced tools
The resolve-from npm package is a module that allows you to resolve the path of a module like require.resolve() but from a given path. It is useful for situations where you need to resolve modules from a different directory than the current file's directory.
Resolve the path of a module from a given path
This feature allows you to resolve the path of a module from a specific directory. You provide the directory as the first argument and the module name as the second argument. It returns the resolved path to the module.
const resolveFrom = require('resolve-from');
const modulePath = resolveFrom('/some/path', 'some-module');
Silently resolve the path of a module or return null
This feature is similar to the first, but it will return null instead of throwing an error if the module cannot be found. This is useful for cases where you want to check for a module's existence without causing an exception.
const resolveFrom = require('resolve-from');
const modulePath = resolveFrom.silent('/some/path', 'non-existent-module');
The 'resolve' package is similar to 'resolve-from' in that it helps to resolve the path of a module. However, 'resolve' provides more options and flexibility, such as the ability to customize the resolution algorithm or to resolve based on the package.json's 'main' field.
The 'enhanced-resolve' package is used by webpack and offers a highly configurable resolution mechanism that can resolve files, directories, and modules. It is more complex and feature-rich compared to 'resolve-from', which is simpler and more straightforward.
The 'browser-resolve' package is designed to resolve modules for browser environments, taking into account the 'browser' field in package.json. It is similar to 'resolve-from' but is specifically tailored for browser-based module resolution.
Resolve the path of a module like
require.resolve()
but from a given path
Unlike require.resolve()
it returns null
instead of throwing when the module can't be found.
$ npm install --save resolve-from
const resolveFrom = require('resolve-from');
// there's a file at `./foo/bar.js`
resolveFrom('foo', './bar');
//=> '/Users/sindresorhus/dev/test/foo/bar.js'
Type: string
Directory to resolve from.
Type: string
What you would use in require()
.
Create a partial using a bound function if you want to require from the same fromDir
multiple times:
const resolveFromFoo = resolveFrom.bind(null, 'foo');
resolveFromFoo('./bar');
resolveFromFoo('./baz');
MIT © Sindre Sorhus
FAQs
Resolve the path of a module like `require.resolve()` but from a given path
The npm package resolve-from receives a total of 84,453,224 weekly downloads. As such, resolve-from popularity was classified as popular.
We found that resolve-from demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.
Security News
Biden's executive order pushes for AI-driven cybersecurity, software supply chain transparency, and stronger protections for federal and open source systems.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.